﻿Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.Params("Tag") = Nothing Or Request.Params("Tag") = "" Then
            ' dsWeightLossProject.SelectCommand = "SELECT * FROM tblProgress ORDER BY ID DESC"
            dsWeightLossProject.SelectCommand = "SELECT tblProgress.ID, tblProgress.UserID, tblProgress.UserName, tblProgress.Title, tblProgress.Description, tblProgress.Picture, tblProgress.Weight, tblProgress.EntryDate, Count(*) AS CommentsCount FROM tblProgress LEFT JOIN tblComments ON tblProgress.ID = tblComments.Progress_FKID GROUP BY tblProgress.ID, tblProgress.UserID, tblProgress.UserName, tblProgress.Title, tblProgress.Description, tblProgress.Picture, tblProgress.Weight, tblProgress.EntryDate ORDER BY tblProgress.ID DESC"
        Else
            dsWeightLossProject.SelectCommand = "SELECT DISTINCT [UserName], [Title], [UserID], [Description], [Picture], [Weight], [EntryDate], [ID], [CommentsCount] FROM [ProgressTagView] WHERE ([Tag] = ?) ORDER BY [ID] DESC"
        End If




        'First, read data from SqlDataSource
        Dim cloudData As DataView = CType(dsTagsByWeight.Select(DataSourceSelectArguments.Empty), DataView)
        Const SpacerMarkup As String = " " 'The markup injected between each item in the cloud
        Dim FontScale() As String = {"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"}

        'All database column names are centralized here. To customize this, simply modify the column names here
        Const WeightColumnName As String = "TagCount"
        Const TextColumnName As String = "Tag"
        Const NavigateUrlColumnName As String = "Tag"
        Const NavigateUrlFormatString As String = "~/Default.aspx?Tag={0}"
        Dim minWeight As Decimal = Decimal.MaxValue, maxWeight As Decimal = Decimal.MinValue

        For Each row As DataRowView In cloudData
            Dim numProductsObj As Object = row(WeightColumnName)
            If Not Convert.IsDBNull(numProductsObj) Then
                Dim numProductsDec As Decimal = Convert.ToDecimal(numProductsObj)

                If numProductsDec < minWeight Then minWeight = numProductsDec
                If numProductsDec > maxWeight Then maxWeight = numProductsDec
            End If
        Next

        Dim scaleUnitLength As Decimal = (maxWeight - minWeight + 1) / Convert.ToDecimal(FontScale.Length)
        For Each row As DataRowView In cloudData
            Dim numProductsObj As Object = row("TagCount")
            If Not Convert.IsDBNull(numProductsObj) Then
                Dim numProductsDec As Decimal = Convert.ToDecimal(numProductsObj)

                Dim scaleValue As Integer = Math.Truncate((numProductsDec - minWeight) / scaleUnitLength)
                CloudMarkup.Text &= String.Format("<a href=""{0}"" style=""font-size:{1};"">{2}</a>{3}", _
                                             Page.ResolveUrl(String.Format(NavigateUrlFormatString, row(NavigateUrlColumnName).ToString())), _
                                             FontScale(scaleValue), row(TextColumnName).ToString(), SpacerMarkup)
            End If
        Next





    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
        Dim searchQuery As String
        searchQuery = CStr(txtSearch.Text)
        Response.Redirect("results.aspx?Query=" & searchQuery)
    End Sub

End Class



   